package com.xhsj.user.utils; /**
 * Copyright &copy; 2012-2016 <a href="http://www.stars-soft.com/">EziCloud</a> All rights reserved.
 */


import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.security.SecureRandom;
import java.util.UUID;

/**
 * 封装各种生成唯一性ID算法的工具类.
 * @author 苏小林
 * @version
 */
@Service
@Lazy(false)
public class IdGen {

	private static SecureRandom random = new SecureRandom();
	
	/**
	 * 封装JDK自带的UUID, 通过Random数字生成, 中间无-分割.
	 */
	public static String uuid() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}
	
	/**
	 * 使用SecureRandom随机生成Long.
	 */
	public static long randomLong() {
		return Math.abs(random.nextLong());
	}

	/**
	 * 生成雪花算法id
	 * @author 苏小林
	 * @date 2020/8/12 9:44
	 * @return: long
	 */
	public static long snowId() {
		return SnowflakeIdWorker.getInstance().nextId();
	}

	/**
	 *生成 uuid， 即用来标识一笔单，也用做 nonce_str
	 * @return
	 */
	public static String generateUUID(){
		return UUID.randomUUID().toString().replaceAll("-","").substring(0,32).toUpperCase();
	}
	
//	public static void main(String[] args) {
//		for (int i=0; i<1000; i++){
//			System.out.println(IdGen.uuid());
//		}
////		System.out.println(IdGen.uuid().length());
////		System.out.println(new IdGen().getNextId());
////		for (int i=0; i<1000; i++){
////			System.out.println(IdGen.randomLong() + "  " + IdGen.randomBase62(5));
////		}
//	}

}
